package com.ccctop.cloud.service.impl;

import com.ccctop.cloud.mapper.TFundRequestDao;
import com.ccctop.crowd.entity.po.TFundRequest;
import com.ccctop.crowd.entity.po.TFundRequestInfo;
import com.ccctop.cloud.mapper.TFundRequestInfoDao;
import com.ccctop.cloud.service.api.TFundRequestInfoService;
import com.ccctop.crowd.util.ResultEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TFundRequestInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-03-07 14:29:59
 */
@Service("tFundRequestInfoService")
public class TFundRequestInfoServiceImpl implements TFundRequestInfoService {
    @Resource
    private TFundRequestInfoDao tFundRequestInfoDao;
    @Resource
    private TFundRequestDao tFundRequestDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TFundRequestInfo queryById(Integer id) {
        return this.tFundRequestInfoDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<TFundRequestInfo> queryAllByLimit(int offset, int limit) {
        return this.tFundRequestInfoDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tFundRequestInfo 实例对象
     * @return 实例对象
     */
    @Override
    public TFundRequestInfo insert(TFundRequestInfo tFundRequestInfo) {
        this.tFundRequestInfoDao.insert(tFundRequestInfo);
        return tFundRequestInfo;
    }

    /**
     * 修改数据
     *
     * @param tFundRequestInfo 实例对象
     * @return 实例对象
     */
    @Override
    public TFundRequestInfo update(TFundRequestInfo tFundRequestInfo) {
        this.tFundRequestInfoDao.update(tFundRequestInfo);
        return this.queryById(tFundRequestInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tFundRequestInfoDao.deleteById(id) > 0;
    }

    @Override
    public TFundRequestInfo selectByProjectId(Integer projectId) throws Exception {
        TFundRequestInfo query = new TFundRequestInfo();
        query.setProjectId(projectId);
        TFundRequestInfo result = tFundRequestInfoDao.selectByProjectId(query);
        return result;
    }

    @Override
    public void agreeIncrease(Integer itemId) {
        TFundRequest tFundRequest = tFundRequestDao.queryById(itemId);
        Integer fundRequestInfoId = tFundRequest.getInfoId();
        tFundRequestInfoDao.agreeIncrease(fundRequestInfoId);
        //检查投票人数的情况
        checkVoteStatus(fundRequestInfoId);
    }

    @Override
    public void refuseIncrease(Integer itemId) {
        TFundRequest tFundRequest = tFundRequestDao.queryById(itemId);
        Integer fundRequestInfoId = tFundRequest.getInfoId();
        tFundRequestInfoDao.refuseIncrease(fundRequestInfoId);
        checkVoteStatus(fundRequestInfoId);
    }

    public void checkVoteStatus(Integer fundRequestInfoId){
        TFundRequestInfo fundRequestInfo = tFundRequestInfoDao.queryByItemId(fundRequestInfoId);
        if(fundRequestInfo.getStatus().equals(0)){
            Integer agreeNum = fundRequestInfo.getAgreeNum();
            Integer refuseNum = fundRequestInfo.getRefuseNum();
            Integer totalMemberNum = fundRequestInfo.getTotalMemberNum();
            int temp = totalMemberNum>>1 + 1;
            if (agreeNum>=temp){
                //更新状态为已成功
                TFundRequestInfo info = new TFundRequestInfo();
                info.setStatus(1);
                info.setId(fundRequestInfoId);
                tFundRequestInfoDao.update(info);
            }else if (refuseNum>=temp){
                //更新状态为已失败
                TFundRequestInfo info = new TFundRequestInfo();
                info.setStatus(2);
                info.setId(fundRequestInfoId);
                tFundRequestInfoDao.update(info);
            }
        }
    }
    @Override
    public TFundRequestInfo selectById(Integer id) {
        return tFundRequestInfoDao.queryById(id);
    }
}